package cn.wolfcode.service.impl;

import cn.wolfcode.domian.Result;
import cn.wolfcode.domian.emp;
import cn.wolfcode.domian.pageBean;
import cn.wolfcode.mapper.empMapper;
import cn.wolfcode.service.empService;
import cn.wolfcode.utils.JwtUtlis;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
@Service
public class empServiceImpl implements empService {
    @Autowired
    private empMapper mapper;

    @Override
    public pageBean list(Integer page, Integer pagesize, String name, Integer gender, Date begin, Date end) {
        PageHelper.startPage(page,pagesize);
        List<emp> list = mapper.list(name,gender,begin,end);
        Page<emp> p = (Page<emp>) list;
        return new pageBean(p.getTotal(),p.getResult());
    }

    @Override
    public void delete(List<Long> ids) {
         mapper.delete(ids);
    }

    @Override
    public void add(emp emp) {
         mapper.add(emp);
    }

    @Override
    public emp getById(Long id) {
        return mapper.getById(id);
    }

    @Override
    public void update(emp emp) {
         mapper.update(emp);
    }

    @Override
    public Result login(emp emp) {
    emp emp1 = mapper.login(emp);
    if (emp1 != null){
        HashMap<String,Object> Claims = new HashMap<>();
        Claims.put("id",emp1.getId());
        Claims.put("username",emp1.getUsername());
        return Result.success(JwtUtlis.createJwt(Claims));
    }
        return Result.error("用户名或密码错误");
    }


}
